﻿#include "GUIPickInfo.h"

// App
#include "FITK_Kernel/FITKAppFramework/FITKRunTimeSetting.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"

// Operator
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/GraphInteractionOperator.h"

#define  PickObjTypeStr      "PickObjType"
#define  PickMethodStr       "PickMethod"
#define  AutoClearStr        "EnableClear"

namespace GUI
{
    void GUIPickInfo::ClearPickInfo()
    {
        auto RTsetting = FITKAPP->getGlobalData()->getRunTimeSetting();
        if (!RTsetting)
        {
            return;
        }

        // 从运行时设置中获取变量值。
        GUIPickInfo::PickObjType pot = (GUIPickInfo::PickObjType)RTsetting->getValueT<int>(PickObjTypeStr);
        GUIPickInfo::PickMethod pm = (GUIPickInfo::PickMethod)RTsetting->getValueT<int>(PickMethodStr);

        // 移除运行参数。
        RTsetting->removeVarient(PickObjTypeStr);
        RTsetting->removeVarient(PickMethodStr);
        RTsetting->removeVarient(AutoClearStr);

        // 关闭拾取方法。
        EventOper::GraphInteractionOperator* operPick = Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::GraphInteractionOperator>("GraphPick");
        if (operPick)
        {
            // 修改对象可拾取性。（性能优化，防止状态销毁重复遍历）
            if (pot != GUIPickInfo::PickObjType::POBJNone || pm != GUIPickInfo::PickMethod::PMNone)
            {
                operPick->setActorStateByPickInfo(GUIPickInfo::PickObjType::POBJNone, GUIPickInfo::PickMethod::PMNone);
            }

            // 清除拾取高亮效果。
            operPick->clear();
        }
    }

    void GUIPickInfo::SetPickInfo(GUIPickInfoStru info, int dataObjId)
    {
        auto RTsetting = FITKAPP->getGlobalData()->getRunTimeSetting();
        if (!RTsetting)
        {
            return;
        }

        // 设置运行参数。
        RTsetting->setValue(PickObjTypeStr, (int)info._pickObjType);
        RTsetting->setValue(PickMethodStr, (int)info._pickMethod);
        RTsetting->setValue(AutoClearStr, (bool)info._autoClear);

        // 开启拾取方法。
        EventOper::GraphInteractionOperator* operPick = Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::GraphInteractionOperator>("GraphPick");
        if (operPick)
        {
            // 清除拾取高亮效果。
            operPick->clear();

            operPick->setActorStateByPickInfo(info._pickObjType, info._pickMethod, dataObjId);
        }
    }

    GUIPickInfoStru GUIPickInfo::GetPickInfo()
    {
        GUIPickInfoStru pickInfo;

        auto RTsetting = FITKAPP->getGlobalData()->getRunTimeSetting();
        if (!RTsetting)
        {
            return pickInfo;
        }

        // 从运行时设置中获取变量值。
        pickInfo._pickObjType = (GUIPickInfo::PickObjType)RTsetting->getValueT<int>(PickObjTypeStr);
        pickInfo._pickMethod = (GUIPickInfo::PickMethod)RTsetting->getValueT<int>(PickMethodStr);
        pickInfo._autoClear = RTsetting->getValueT<bool>(AutoClearStr);

        return pickInfo;
    }
}
